/*  利用运算符重载，使Point对象能够进行+、+=、前置++、后置++操作（对坐标进行计算）*/
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;

//Point类
class Point{
private:
    int m_x;
    int m_y;
public:
    //将+重载声明友元
    friend
        Point operator+(const Point & point1, const Point & point2);
    //将<<重载声明友元
    friend
        ostream & operator<<(ostream & os, const Point & point);
    //将-重载声明友元
    friend
        Point operator-(const Point & point1, const Point & point2);
    Point(int x = 0, int y = 0)
        :m_x(x),
        m_y(y)
    {}
    //++
    //前++
    Point & operator++() {
        m_x++;
        m_y++;
        return *this;
    }
    //后++
    Point operator++(int) {
        class Point temp(m_x,m_y);
        m_x++;
        m_y++;
        return temp;
    }
    //--
    //前--
    Point & operator--() {
        m_x--;
        m_y--;
        return *this;
    }
    //后++
    Point operator--(int) {
        class Point temp(m_x,m_y);
        m_x--;
        m_y--;
        return temp;
    }

    //+=
    Point & operator+=(const Point & point) {
        m_x += point.m_x;
        m_y += point.m_y;
        return *this;
    }
    //-=
    Point & operator-=(const Point & point) {
        m_x -= point.m_x;
        m_y -= point.m_y;
        return *this;
    }

};//end of class Point
  //+
Point operator+(const Point & point1, const Point & point2) {
    return {point1.m_x + point2.m_x, point1.m_y + point2.m_y};
}
//-
Point operator-(const Point & point1, const Point & point2) {
    return Point(point1.m_x-point2.m_x, point1.m_y-point2.m_y);
}
//<<输出运算符
ostream & operator<<(ostream & os, const Point & point) {
    os << "("<< point.m_x << "," << point.m_y << ")";
    return os;
}
int main()
{
    Point point1(1,1); 
    Point point2(2,2);
    //测试+
    Point point3 = point1 + point2 + point1;
    cout << point3 << endl;
    //测试+=
    point1 += point2;
    cout << point1 << endl;
    //测试++
    cout << point2++ << endl;
    cout << ++point2 << endl;
    //测试--
    cout << point2-- << endl;
    cout << --point2 << endl;

    return 0;
}

